package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.Topic;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class SpecificTopicAdapter extends RecyclerView.Adapter<SpecificTopicAdapter.SpecificTopicViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<Topic> topicList;

    private mItemClickListener itemClickListener;

    public SpecificTopicAdapter(Context context, List<Topic> topicList) {
        this.context = context;
        this.topicList = topicList;

        inflater = LayoutInflater.from(context);
    }

    /**
     * 设置监听事件
     * @param listener
     */
    public void setOnItemClickListener(mItemClickListener listener)
    {
        this.itemClickListener = listener;
    }

    @Override
    public SpecificTopicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = inflater.inflate(R.layout.adapter_specific_topic,null);

        return new SpecificTopicViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(SpecificTopicViewHolder holder, final int position) {

        Topic topic = topicList.get(position);
        ImageLoader.getInstance().displayImage(topic.getImage_url(),holder.imageView, ImageUtil.getDefaultOptions());
        holder.tvtitle.setText(topic.getTitle());
        holder.tvnum.setText(topic.getContent_num()+"");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return topicList == null ? 0 : topicList.size();
    }

    public class SpecificTopicViewHolder extends RecyclerView.ViewHolder {

        TextView tvtitle,tvnum;
        ImageView imageView;

        public SpecificTopicViewHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.specifictopic_adapter_main_iv);
            tvtitle = (TextView) itemView.findViewById(R.id.specifictopic_adapter_title_tv);
            tvnum = (TextView) itemView.findViewById(R.id.specifictopic_adapter_num_tv);
        }
    }

    public interface mItemClickListener
    {
        void onItemClick(int position);
    }
}
